#include <stdio.h>
#include <math.h>

int stack[120]={0};
int top=0;
int hop=0;
int tmp[1000000]={0};

void push(int n)
{
  tmp[top++]=n;
}

int pop()
{
  if (top == 0)return tmp[0];
  return tmp[--top];
}

void swap (char *x, char *y)
{
  char temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

void permute(char *a, int i, int n)
{
  int j;
  if (i == n){
    stack[hop++]=(a[0]-'0')*10000+(a[1]-'0')*1000+(a[2]-'0')*100+(a[3]-'0')*10+(a[4]-'0');
    //printf("%d ", stack[hop-1]);
  }
  else
    {
      for (j = i; j <= n; j++)
	{
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));
	}
    }
}

int main()
{
  int tri, squ=32, pen=26, hex=23, hep=21, oct=19;
  int next, max, min, sum=0, count=0, p, i, j, k, l, m;
  int matrix[9][100]={0};
  
  for (i=19; i<=58; i++) {
    p = i*(3*i-2);
    if (p%100 >= 10){
      matrix[8][count++]=p;
      //push(p);
      //printf("%d ", p);			
    }
  }
  
  count=0;
  for (i=20; i<=63; i++) {
    p = i*(5*i-3)/2;
    if (p>1000 && p%100 >= 10){
      matrix[7][count++]=p;
    }
  }
  
  count=0;
  for (i=23; i<=70; i++) {
    p = i*(2*i-1);
    if (p%100 >= 10){
      matrix[6][count++]=p;
    }
  }
  
  count=0;
  for (i=26; i<=81; i++) {
    p = i*(3*i-1)/2;
    if (p%100 >= 10){
      matrix[5][count++]=p;
    }
  }
  
  count=0;
  for (i=32; i<=99; i++) {
    p = i*i;
    if (p%100 >= 10){
      matrix[4][count++]=p;
      //printf("%d ", p);
    }
  }printf("\n");
  
  count=0;
  for (i=45; i<=140; i++) {
    p = i*(i+1)/2;
    if (p%100 >= 10){
      matrix[3][count++]=p;
      //printf("%d ", p);
    }
  }
  
  char a[]="76543";
  permute(a, 0, 4);
  printf("%d \n", tmp[top-1]);
  int row,t, depth=0;
  for (i=0; i<30; i++){
    p = matrix[8][i];
    for (j=0; j<120; j++) {
      row = stack[j];
      while(top >0) {
	k=0;
	while(matrix[row%10][k]>0){
	  if (p%100 == matrix[row%10][k]/100)
	    push(matrix[row%10][k]);
	}
	row/=10;
      }
    }
  }


  for (i=0; i<120; i++) {
    row = stack[i];
    t = top-1;
    while(top >0) {
      max = tmp[top-1];
      //printf("%d ", max);
      while(row >0) {
	p = pop();
	//printf("%d %d", p, row);return;
	k=0;
	while(matrix[row%10][k]>0) {
	  if (p%100 == matrix[row%10][k]/100) { 
	    push(matrix[row%10][k]);
	    printf("%d ", matrix[row%10][k]);
	  }
	  k++;
	}//printf("%d\n", k);return;
	row /=10;
	if (row == 0 && matrix[row%10][k-1]%100==max/100){printf("%d\n", max);return;}
      }//printf("top %d ", tmp[top-1]);return;
      
    }
  }
  printf("");
}

/*triP[88], squP[53], penP[47], hexP[44], hepP[40], octP[30], */
